//
//  MyNETLunBoTu.m
//  SmileRead
//
//  Created by dllo on 15/8/26.
//  Copyright (c) 2015年 JIZ. All rights reserved.
//

#import "MyNETLunBoTu.h"
#import "Path.h"
#import "VoiceModel.h"

@interface MyNETLunBoTu ()<UIScrollViewDelegate>

{
    NSInteger _count;
}

@property (nonatomic, retain) UIScrollView *scroll;

@property (nonatomic, retain) NSMutableArray *arrayOfItem;

@property (nonatomic, retain) UIPageControl *pageControl;

// 记录图片的下标
@property (nonatomic, assign) NSInteger index;

@property (nonatomic, retain) NSTimer *timer;

@end

@implementation MyNETLunBoTu

- (instancetype)initWithFrame:(CGRect)frame arrayOfImage:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        self.arrayOfItem = [NSMutableArray array];
        [self.arrayOfItem addObjectsFromArray:array];
        [self createSubView];
    }
    return self;
}

- (void)createSubView
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerOfImage) userInfo:nil repeats:YES];
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.scroll.contentSize = CGSizeMake(self.scroll.frame.size.width * self.arrayOfItem.count, 0);
    self.scroll.delegate = self;
    self.scroll.pagingEnabled = YES;
    self.scroll.contentOffset = CGPointMake(self.frame.size.width, 0);
    self.scroll.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.scroll];
    
    for (int i = 0; i < self.arrayOfItem.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.scroll.frame.size.width, 0, self.scroll.frame.size.width, self.scroll.frame.size.height - 20 * heightFit)];
        imageView.userInteractionEnabled = YES;
        
        // 从网络获取
        VoiceModel *model = self.arrayOfItem[i];
        [Path pathWithImageView:imageView URLString:model.coverimg];
        
        // 为imageView添加点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Clicked:)];
        [imageView addGestureRecognizer:tap];
        [tap release];
        
        [self.scroll addSubview:imageView];
        [imageView release];
    }
    [_scroll release];
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(280 * widthFit, self.scroll.frame.size.height - 20 * heightFit, self.frame.size.width - 280 * widthFit, 20 * heightFit)];
    [self.pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
    self.pageControl.pageIndicatorTintColor = [UIColor blackColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
    [self addSubview:self.pageControl];
    [_pageControl release];
    self.pageControl.numberOfPages = self.arrayOfItem.count - 2;
}

- (void)timerOfImage
{
    self.scroll.contentOffset = CGPointMake(_count * self.scroll.frame.size.width, 0);
    self.pageControl.currentPage = _count;
    _count = _count + 1;
    if (_count == self.arrayOfItem.count - 2) {
        _count = 0;
    }
}


// 点击手势的方法
- (void)Clicked:(UITapGestureRecognizer *)tap
{
    // 将图片下标传过去, 来为首页确定跳转的页面
    NSNumber *number = [NSNumber numberWithInteger:self.index];
    [self.target performSelector:self.action withObject:number];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == 0) {
        
        scrollView.contentOffset = CGPointMake((self.arrayOfItem.count - 2) * self.frame.size.width, 0);
           }
    if (scrollView.contentOffset.x >= (self.arrayOfItem.count - 1) * self.frame.size.width) {
        scrollView.contentOffset = CGPointMake( self.frame.size.width, 0);
    }
    self.pageControl.currentPage = scrollView.contentOffset.x / self.frame.size.width - 1;
    
    NSLog(@"%f", scrollView.contentOffset.x / self.frame.size.width - 1);
    
    self.index = self.pageControl.currentPage;
    // 通知代理执行方法
    [self.delegate sendWithIndex:self.pageControl.currentPage + 1];
    
}

- (void)pageChange:(UIPageControl *)page
{
    self.scroll.contentOffset = CGPointMake((page.currentPage + 1) * self.frame.size.width, 0);
    
    // 通知代理执行方法
    self.index = page.currentPage;
    [self.delegate sendWithIndex:page.currentPage + 1];
}

- (void)dealloc
{
    [self.timer invalidate];
    self.timer = nil;
    [_pageControl release];
    [_arrayOfItem release];
    [_scroll release];
    [super dealloc];
}


@end
